package com.huafeng.serviceutil.exception;

import com.huafeng.model.base.Result;
import com.huafeng.model.enums.ResultCodeEnum;
import com.huafeng.model.exception.BusinessException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @author 花风
 * @date 2023/8/15 21:08
 * @Description 全局异常处理器
 */
@RestControllerAdvice
public class GlobalExceptionHandler {
    private final Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);

    @ExceptionHandler(BusinessException.class)
    public Result businessExceptionHandle(BusinessException businessException){
        //打印错误日志
        logger.error("businessException: " + businessException.getMessage(),businessException);
        return new Result(businessException.getMessage(),businessException.getCode());
    }

    @ExceptionHandler(RuntimeException.class)
    public Result runtimeExceptionHandle(RuntimeException runtimeException){
        //打印错误日志
        logger.error("runtimeException：{}",runtimeException);
        return new Result(runtimeException.getMessage(), ResultCodeEnum.SERVICE_ERROR.getCode());
    }
}
